package demo.practice.leetcode_offer;

import demo.practice.TreeNode;

public class Offer26 {


    public boolean isSubStructure(TreeNode A, TreeNode B) {

        if(B==null)
            return false;

        if (A.val == B.val) {
            if (check(A, B))
                return true;
        }

        if (A.left != null) {
            if (isSubStructure(A.left, B))
                return true;
        }

        if (A.right != null) {
            return isSubStructure(A.right, B);
        }


        return false;
    }

    private boolean check(TreeNode aroot, TreeNode broot) {

        if(broot==null|| aroot==null){
            return false;
        }

        if(aroot.val!=broot.val)
            return false;

        if(broot.left!=null){
            if(!check(aroot.left,broot.left))
                return false;
        }


        if(broot.right!=null){
            return check(aroot.right, broot.right);
        }

        return true;

    }
}
